package com.yonxao.demo.crud.config.swagger;

import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * swagger配置
 *
 * @author yonxao
 * @date 2021/3/22
 */
@EnableConfigurationProperties(SwaggerProperties.class)
@EnableSwagger2
@Configuration
public class SwaggerConfiguration {

    @Bean
    public Docket creatRestApi(SwaggerProperties swaggerProperties, ApiInfo apiInfo) {
        //指定是否开启swagger
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo).enable(true).select()
                .apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
                //指定rest接口所在的位置
                // .apis(RequestHandlerSelectors.basePackage(swaggerProperties.getBasePackage()))
                //指定要测试哪些api
                .paths(PathSelectors.any())
                .build()
                .pathMapping("/");
    }

    @Bean
    public ApiInfo apiInfo(SwaggerProperties swaggerProperties) {
        //指定页面的大标题
        return new ApiInfoBuilder().title(swaggerProperties.getTitle())
                //关于这个测试页面的描述
                .description(swaggerProperties.getDescription())
                //指定作者的相关信息
                .contact(new Contact(swaggerProperties.getAuthor(), swaggerProperties.getUrl(), swaggerProperties.getEmail()))
                //指定版本并创建
                .version(swaggerProperties.getVersion()).build();
    }

    @Bean
    public EnumModelPropertyBuilderPlugin enumModelPropertyBuilderPlugin() {
        return new EnumModelPropertyBuilderPlugin();
    }

    @Bean
    public EnumParameterBuilderPlugin enumParameterBuilderPlugin() {
        return new EnumParameterBuilderPlugin();
    }
}

